package com.example.backend.service;

import com.example.backend.entity.Car;
import com.example.backend.entity.DingDanDetail;
import com.example.backend.mapper.CarMapper;
import com.example.backend.mapper.DingDanDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DingDanDetailService {

    @Autowired
    private DingDanDetailMapper dingDanDetailMapper;

    @Autowired
    private CarMapper carMapper;


    public ArrayList<DingDanDetail> selectDingDanDetails() {
        return dingDanDetailMapper.selectDingDanDetails();
    }

    public int deleteByOrderNum(String num) {
        return dingDanDetailMapper.deleteByDingDanNum(num);
    }

    public int add(Car car) {
        int i;
        DingDanDetail dingDanDetail = new DingDanDetail();
        ArrayList<Car> cars = carMapper.selectCars(car);
        for (Car c : cars) {
            dingDanDetail.setDish_name(c.getDish_name());
            dingDanDetail.setNum(c.getNum());
            dingDanDetail.setPrice(c.getMoney());
            dingDanDetail.setDingdan_num(car.getCar_num());
            i=dingDanDetailMapper.insert(dingDanDetail);
            car.setMoney(0.0);
            carMapper.updateShopNum(car);
            if (i<0){
                return -1;
            }
        }

        return 1;
    }

    public List<DingDanDetail> selectDingDanDetailsByDingDanNum(String num) {
        return dingDanDetailMapper.selectDingDanDetailsByDingDanNum(num);
    }
}
